<?php
	# If user is not signed in, redirect to index page
	if(!isset($_COOKIE['acct']['id']) or !isset($_COOKIE['acct']['email'])) {
		header('refresh: 0.1; url="http://localhost/microwebph/"'); return;
	}
	
	# Set unlimited time on processing requests
	set_time_limit(0);
	
	# If signed in with a cookie url 'jump'
	if(!empty($_COOKIE['jump'])) {
		$url = $_COOKIE['jump'];
		# remove cookie 'jump'
		setcookie('jump', null);
		# redirect to that url
		header("refresh: 0.1; url=\"{$url}\""); return;
	}
	
	# Instantiate a new mysql connection
	$mysqli = new mysqli('localhost', 'prime', 'pr1m34dm1n', 'MicroWebPH');
	
	# An error occurred on establishing mysql connection
	if(mysqli_connect_errno()) {
		# redirect to home page
		header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view"'); return;
	}
	
	# set variables based on area of access
	# Microorganism
	if(isset($_GET["micro"])) {
		$area = "microorganism";
		$optn = "micro";
	}
	# Image
	elseif(isset($_GET["img"])) {
		$area = "image";
		$optn = "img";
	}
	# Document
	elseif(isset($_GET["doc"])) {
		$area = "document";
		$optn = "doc";
	}
	# Location
	elseif(isset($_GET["loc"])) {
		$area = "location";
		$optn = "loc";
	}
	# Resource
	elseif(isset($_GET["rsrc"])) {
		$area = "resource";
		$optn = "rsrc";
	}
	# Project
	elseif(isset($_GET["proj"])) {
		$area = "project";
		$optn = "proj";
	}
	# Editor
	elseif(isset($_GET["edtr"])) {
		$area = "editor";
		$optn = "edtr";
	}
	elseif($_COOKIE['acct']['type']=='PRIME') {
		# Account
		if(isset($_GET["acct"])) {
			$area = "account";
			$optn = "acct";
		}
		# Application
		elseif(isset($_GET["appn"])) {
			$area = "application";
			$optn = "appn";
		}
	}
	# Uncategorized area, redirect to home page
	else {
		header('refresh: 0.1; url="http://localhost/microwebph/main.php?micro=view"'); return;
	}
	
	$_REQUEST['optn'] = $optn;
	$_REQUEST['area'] = $area;
?>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	<title><?php echo ucfirst($area).'s' ?> | MicroWebPH</title>
	<base href="http://localhost/microwebph/"/>
	
	<link href="style/jquery-ui-1.8.18.custom.css" type="text/css" rel="stylesheet"/>
	<script src="script/jquery-1.7.1.min.js" type="text/javascript"></script>
	<script src="script/jquery-ui-1.8.18.custom.min.js" type="text/javascript"></script> <?php
	
	# If area is micro, proj, or location, include Google Maps
	if(in_array($optn, array('micro','proj','loc')) and !isset($_GET['map'])) { ?>
		<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>
		<script src="script/jquery.gmap3.min.js" type="text/javascript"></script> <?php
	} ?>
	
	<script src="script/jquery.livequery.js" type="text/javascript"></script>
	<script src="script/ajaxfileupload.js" type="text/javascript"></script>
	
	<link href="style/mystyle.css" type="text/css" rel="stylesheet"/>
	<script src="script/functions.js" type="text/javascript"></script>
	<script src="script/myscript.js" type="text/javascript"></script>
</head>

<body>
	<div id="cloak">
		<img src="template/loading.gif" class="loading-gif" alt="Loading..."/>
		<div class="time"></div>
		<div class="elapsed"></div>
	</div>
	<div id="coat"></div>
	<div id="mask"></div>
	
	<div id="head">
		<div id="top"></div>
		<div id="nav-active" tabindex="0"><?php echo ucfirst($area) ?>s</div>
		<div id="navs">
			<a class="nav" href="main.php?micro=view"><div id="nav-micro" class="picto nav" tabindex="-1">Microorganisms</div></a>
			<a class="nav" href="main.php?img=view"><div id="nav-img" class="picto nav" tabindex="-1">Images</div></a>
			<a class="nav" href="main.php?doc=view"><div id="nav-doc" class="picto nav" tabindex="-1">Documents</div></a>
			<a class="nav" href="main.php?loc=view"><div id="nav-loc" class="picto nav" tabindex="-1">Locations</div></a>
			<a class="nav" href="main.php?rsrc=view"><div id="nav-rsrc" class="picto nav" tabindex="-1">Resources</div></a>
			<a class="nav" href="main.php?proj=view"><div id="nav-proj" class="picto nav" tabindex="-1">Projects</div></a>
			<a class="nav" href="main.php?edtr=view"><div id="nav-edtr" class="picto nav" tabindex="-1">Editors</div></a> <?php
			
			# If current user is prime, include links to Account and Application
			if($_COOKIE['acct']['type']=="PRIME") { ?>
				<a class="nav" href="main.php?acct=view"><div id="nav-acct" class="picto nav" tabindex="-1">Accounts</div></a>
				<a class="nav" href="main.php?appn=view"><div id="nav-appn" class="picto nav" tabindex="-1">Applications</div></a> <?php
			} ?>
		</div>
		
		<span id="htitle" data-tool="{'head':'','x':'l'}" <?php if(!empty($_REQUEST['q'])) { echo 'style="width:500px"'; } ?>></span>
		
		<div id="dial"> <?php
			require_once "template/search.php"; ?>
			<a tabindex="0" id="my-acct" data-modal="#mod-myacct" class="picto"
				data-tool="{'head':'<?php echo $_COOKIE['acct']['email'] ?>',
				'body':'
					<span id=\'editmyacct\'><a data-modal=\'#mod-myacct\'>Edit</a></span>
					<span id=\'signout\'><a href=\'sign.php?out\'>Sign out</a></span>
				',
				'stay':'true','behav':'More'}"></a>
			
			<span class="expcol">
				<button type="button" class="expand all" data-content="<?php echo $optn ?>" title="Expand all"></button>
				<button type="button" class="collapse all" data-content="<?php echo $optn ?>" title="Collapse all"></button>
			</span>
		</div>
	</div>
	
	<div id="main">
		<?php
			# Process request based on area
			require_once "process.php";
		?>
	</div>
	
	<div id="modals">
		<div id="mod-myacct" class="modal my-acct area">
			<div class="modal-head"><b>My Account</b> <button type="button" class="modal-reject"></button></div>
			
			<div class="modal-body alright"> <?php
				$acct = new Account($mysqli, $_COOKIE['acct']['id']);
				
				if($result = $acct->search($_COOKIE['acct']['id'], array('c'=>'id'), 0, 1)) {
					$myacct = $result[0]->fetch_object(); ?>
					
					<form id="form-myacct" data-area="#area<?php echo $myacct->ID ?>">
						<label data-for="txtMyEmail">Email:</label>
							<input id="txtMyEmail" type="email" name="email" value="<?php echo $myacct->Email ?>" class="reqd"/><br/>
						<label data-for="spnType">Type:</label>
							<input id="spnType" type="text" value="<?php echo $myacct->Type ?>" class="reqd" readonly="readonly"
								data-tool="{'head':'<?php echo $myacct->Type=='PRIME'
									? "Can manage all items and accounts"
									: ($myacct->Type=='ADMIN' ? "Can manage all items"
										: ($myacct->Type=='EDITOR' ? "Can manage owned and authorized items"
											: ($myacct->Type=='CLIENT' ? "Can view and download some items" : ''))) ?> in the system','x':'l'}"/><br/>
						<label data-for="lnkChangeMyPword">Password:</label>
							<span id="spnMyPwordNew" class="none">
								<input id="txtMyPwordNew" type="password" name="pwordNew" placeholder="New password" class="exclude tipped"/><br/>
								<input id="txtMyPwordNewRep" type="password" name="pwordRep" placeholder="Repeat new password" class="exclude"/><br/>
							</span>
						<a id="lnkChangeMyPword" tabindex="0" class="peel on prev lnkChangePword" style="margin-right:151px">Change current password</a>
						<input id="cbxUpdMyPword" type="checkbox" name="cbxUpdPword" value="<?php echo $myacct->ID ?>" class="cbxUpdPword none"/><br/>
						
						<label data-for="txtMyLname">Last name:</label>
							<input id="txtMyLname" type="text" name="lname" value="<?php echo $myacct->Lname ?>" class="reqd"/><br/>
						<label data-for="txtMyFname">First name:</label>
							<input id="txtMyFname" type="text" name="fname" value="<?php echo $myacct->Fname ?>" class="reqd"/><br/>
						<label data-for="txtMyMname">Middle name:</label>
							<input id="txtMyMname" type="text" name="mname" value="<?php echo $myacct->Mname ?>" class="reqd"/><br/>
						<label data-for="txtMyAddress">Address:</label>
							<input id="txtMyAddress" type="text" name="addrs" value="<?php echo $myacct->Addrs ?>" class="reqd"/><br/>
						
						<input name="id" type="hidden" value="<?php echo $myacct->ID ?>" class="abled"/>
						<input name="type" type="hidden" value="<?php echo $myacct->Type ?>" class="abled"/>
						<input name="report" type="hidden" value="<?php echo $myacct->Report ?>" class="abled"/>
					</form><hr class="bold"/>
					
					<form id="form-myacct-verify" name="form-myacct-verify">
						<label data-for="txtMyPword">Verify:</label>
						<input id="txtMyPword" type="password" name="p" placeholder="Current password" class="reqd" data-tool="{'head':'Confirm changes'}"/><br/>
						<input id="hidMyEmail" type="hidden" name="e" value="<?php echo $myacct->Email ?>"/>
					</form> <?php
				} ?>
			</div>
			
			<div class="modal-dial">
				<span class="left">
					<button type="button" class="clear">Clear</button>
					<button type="reset" class="reset">Reset</button>
				</span>
				
				<span>
					<button type="reset" class="modal-reject cancel">Cancel</button>
					<button type="submit" form="form-myacct-verify">Update</button>
				</span>
			</div>
		</div>
	</div>
</body>

</html> <?php

# Close mysql connection
$mysqli->close();
?>